
rm(list=ls())
library(tidyverse)
library(scales)
library(broom)
#df<-#import data24.csv as df



fig4<-list(
  lm(ftjudcourt~cneg+cpos+pneg+ppos+neg_aff+nat_ft_avg+female+educ+pidc+pid_dk+age+black+hispanic,data=df)%>%
    broom::tidy(.)%>%
    .[c(2,3,4,5),c(1,2,3)]%>%
    mutate(ci_low=estimate-(1.96*std.error),ci_up=estimate+(1.96*std.error),x=ifelse(term=="pneg" | term=="ppos","p","c"),Evaluations="A. Court Evaluations",terms=ifelse(term=="pneg","Police negative",
                                                                                                                                                                          ifelse(term=="ppos","Police positive",ifelse(term=="cneg","Courts negative","Courts positive")))),
  lm(ftpolice~cneg+cpos+pneg+ppos+neg_aff+nat_ft_avg+female+educ+pidc+pid_dk+age+black+hispanic,data=df)%>%
    broom::tidy(.)%>%
    .[c(2,3,4,5),c(1,2,3)]%>%
    mutate(ci_low=estimate-(1.96*std.error),ci_up=estimate+(1.96*std.error),x=ifelse(term=="pneg" | term=="ppos","p","c"),Evaluations="B. Police Evaluations",terms=ifelse(term=="pneg","Police negative",
                                                                                                                                                                           ifelse(term=="ppos","Police positive",ifelse(term=="cneg","Courts negative","Courts positive")))))%>%
  bind_rows(.[[1]], .[[2]])%>%
  ggplot(.,aes(x=estimate,y=terms,xmin=ci_low,xmax=ci_up,shape=terms,group=terms,color=terms))+
  geom_point(size=3)+
  geom_errorbar(aes(linetype=terms),width=.14)+
  xlab("")+
  scale_color_manual(values=c("black","black","black","black"))+
  scale_shape_manual(values=c(19,1,15,0))+
  scale_y_discrete(breaks=c("","","","")) +
  #scale_linetype_manual(values=c(2,2,1,1))+
  scale_x_continuous(n.breaks=16,minor_breaks = 0,limits=c(-8,8))+
  geom_vline(xintercept = 0, linetype = 5,col="grey20")+
  #geom_hline(yintercept = 1.5, linetype = 2)+
  guides(color=guide_legend(title="Experiences",reverse = TRUE),shape=guide_legend(title="Experiences",reverse = TRUE),linetype=guide_legend("Experiences",reverse = TRUE))+
  ylab("")+
  facet_wrap(vars(Evaluations),nrow=2)+
  scale_linetype_manual(values =c("solid","solid","longdash","longdash"))+
  #xlim(-.5,.12)+
  theme_bw()+
  theme(legend.position="bottom")


  fig4

  
